<template>
  <transition name="yg-notice" @after-leave="afterLeave">
    <div class="yg-notice" v-show="show">
      <div class="yg-title">{{title}}</div>
      <div class="yg-content">{{content}}</div>
      <div class="yg-close" @click="handleClose">close</div>
    </div>
  </transition>
</template>

<script>
export default {
  props: {
    title: String,
    content: String,
    show: Boolean,
    id: Number,
    time: {
      type: Number,
      default: 3000
    }
  },
  data() {
    return {
      height: 0
    }
  },
  methods: {
    handleClose() {
      this.$emit('close', this)
    },
    afterLeave() {
      this.$emit('after-leave', this)
    },
    createTime() {
      this.timer = setTimeout(() => {
        this.$emit('close', this)
      }, this.time)
    },
    clearTime() {
      window.clearTime(this.timer)
    },
    getElementHeight() {
      this.height = this.$el.offsetHeight
    }
  },
  mounted() {
    this.createTime()
  }
}
</script>

<style lang="less">
.yg-notice {
  position: relative;
  width: 385px;
  padding: 20px;
  box-sizing: border-box;
  background-color: #fff;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
  margin-bottom: 10px;
}
.yg-close {
  position: absolute;
  top: 0;
  right: 10px;
  cursor: pointer;
}

.yg-notice-enter-active,
.yg-notice-leave-active {
  transition: all 0.5s;
}

.yg-notice-enter,
.yg-notice-leave-to {
  opacity: 0;
  transform: translateX(100%);
}
</style>
